<?php
/**
 * Error decorator for human-readable errors
 */
class App_Form_Decorator_Errors extends Zend_Form_Decorator_Errors
{
    /**
     * Render errors
     *
     * @param  string $content
     * @return string
     */
    public function render($content)
    {
        $element = $this->getElement();
        $view    = $element->getView();
        $label   = $element->getLabel();
        if (null === $view) {
            return $content;
        }

        $errors = $element->getMessages();
        if (empty($errors)) {
            return $content;
        }

        $errors = $this->_convertErrorMessages($label, $errors);

        $separator = $this->getSeparator();
        $placement = $this->getPlacement();
        $errors    = $view->formErrors($errors, $this->getOptions());

        switch ($placement) {
            case self::APPEND:
                return $content . $separator . $errors;
            case self::PREPEND:
                return $errors . $separator . $content;
        }
    }

    /**
     * Convert error codes to something user friendly
     *
     * @param array $errors
     * @return array
     */
    protected function _convertErrorMessages($label, $errors)
    {
        $newErrors = array();

        // Empty string
        if (array_key_exists('stringEmpty', $errors) ||
            array_key_exists('isEmpty', $errors))
        {
            return array($label . ' may not be empty');
        }

        // String too short
        if (array_key_exists('stringLengthTooShort', $errors)) {
            list($string, $min) = sscanf($errors['stringLengthTooShort'],
                                         "%s is less than %d characters long");

            return array($label . ' must be at least ' . $min . ' characters long');
        }

        // String too long
        if (array_key_exists('stringLengthTooLong', $errors)) {
            list($string, $max) = sscanf($errors['stringLengthTooShort'],
                                         "%s is greater than %d% characters long'");

            return array($label . ' may only be ' . $max. ' characters long or shorter');
        }

        // Alnum
        if (array_key_exists('notAlnum', $errors)) {
            return array($label . ' must be alpha-numeric characters');
        }

        // Alpha
        if (array_key_exists('notAlpha', $errors)) {
            return array($label . ' may only contain alpha characters');
        }

        // Invalid email
        if (array_key_exists('emailAddressInvalid', $errors) ||
            array_key_exists('emailAddressInvalidFormat', $errors) ||
            array_key_exists('emailAddressInvalidHostname', $errors) ||
            array_key_exists('emailAddressInvalidMxRecord', $errors) ||
            array_key_exists('emailAddressDotAtom', $errors) ||
            array_key_exists('emailAddressQuotedString', $errors) ||
            array_key_exists('emailAddressInvalidLocalPart', $errors) ||
            array_key_exists('emailAddressLengthExceeded', $errors))
        {
            return array($label . ' is not valid');
        }

        // Invalid URI
        if (array_key_exists('uriInvalid', $errors))
        {
            return array($label . ' is not valid');
        }

        // If we replaced any error messages, return them, else return the
        // original once.
        if (count($newErrors)) {
            return $newErrors;
        } else {
            return $errors;
        }
    }
}
